<?php
require_once("include/bittorrent.php");
if (get_user_class() >= UC_SYSOP) {

function EditCategory() {
	global $tracker_lang;
	if ( empty($_GET['id']) || !($id=intval($_GET['id'])) ) {
		$data=array('title'=>$tracker_lang['error'],'text'=>$tracker_lang['Incorrect ID'],'type'=>'error');
		setcookie('infomsg',serialize($data));
		RedirectTo('admincp.php?op=Categories','script');
	}	
	$query = "SELECT `name`, `image`, `sort` FROM `categories` WHERE `id` = ".intval($id);
	$r=sql_query($query) or sqlerr(__FILE__,__LINE__);
	if ( ! mysql_num_rows($r) ) {
		$data=array('title'=>$tracker_lang['error'],'text'=>$tracker_lang['Incorrect ID'],'type'=>'error');
		setcookie('infomsg',serialize($data));
		RedirectTo('admincp.php?op=Categories','script');	
	}
	else $data=mysql_fetch_assoc($r);
	$name=$data['name'];
	$img=$data['image'];
	$sort=$data['sort'];
?>
<form name="form1" method="get" action="admincp.php">
<div align="center"><input type="hidden" name="id" value="<?=$id;?>"><?=$tracker_lang['Editing'];?>: <?=$tracker_lang['category'];?> <strong>&quot;<?=$name;?>&quot;</strong></div>
<br /><input type="hidden" name="op" value="UpdateCategory">
<table class="main" cellspacing="0" cellpadding="5" width="100%">
<tr><td><?=$tracker_lang['caption'];?>: </td><td align="left"><input type="text" size="50" name="cat_name" value="<?=$name?>"></td></tr>
<tr><td><?=$tracker_lang['image'];?>: </td><td align="left"><input type="text" size="50" name="cat_img" value="<?=$img?>"></td></tr>
<tr><td><?=$tracker_lang['weight'];?>: </td><td align="left"><input type="text" size="50" name="cat_sort" value="<?=$sort?>"></td></tr>
<tr><td></td><td><div align="right"><input type="submit" value="<?=$tracker_lang['edit'];?>"></div></td></tr>
</table></form>
<?php
}

function AddCategory() {
	global $tracker_lang;
	if ( empty($_GET['cat_name']) ) {
		$data=array('title'=>$tracker_lang['error'],'text'=>$tracker_lang['Incorrect name'],'type'=>'error');
		setcookie('infomsg',serialize($data));
		RedirectTo('admincp.php?op=Categories','script');
	}
	$cat_name = htmlspecialchars($_GET['cat_name']);
	$cat_img = htmlspecialchars($_GET['cat_img']);
	$cat_sort = intval($_GET['cat_sort']);
	$query = "INSERT INTO categories SET name = ".sqlesc($cat_name).", image = ".sqlesc($cat_img).",sort = ".sqlesc($cat_sort);
	$sql = sql_query($query) or sqlerr(__FILE__,__LINE__);
	cache_unset('categories');
	$data=array('title'=>$tracker_lang['success'],'text'=>$tracker_lang['Category was added'],'type'=>'success');	
	setcookie('infomsg',serialize($data));
	RedirectTo('admincp.php?op=Categories','script');
}

function DeleteCategory() {
	global $tracker_lang;
	if ( empty($_GET['id']) || !($id=intval($_GET['id'])) ) {
		$data=array('title'=>$tracker_lang['error'],'text'=>$tracker_lang['Incorrect ID'],'type'=>'error');
		setcookie('infomsg',serialize($data));
		RedirectTo('admincp.php?op=Categories','script');
	}
	$query = 'DELETE FROM categories WHERE id='.intval($_GET['id']);
	cache_unset('categories');
	$sql = sql_query($query) or sqlerr(__FILE__,__LINE__);
	$data=array('title'=>$tracker_lang['success'],'text'=>$tracker_lang['Category was deleted'],'type'=>'success');
	setcookie('infomsg',serialize($data));
	RedirectTo('admincp.php?op=Categories','script');
}


function ViewCategories() {
	global $tracker_lang,$options;
	ShowInformationMessage();
?>
<style>
.hidden {
	display:none;
}
.selected {
	color:green;
}
.viewcategories {
	text-decoration:none;
	font-size:14px;
	cursor:pointer;
	font-weight:bold;
}
.addcategory {
	text-decoration:none;
	font-size:14px;
	cursor:pointer;
	font-weight:bold;
}
</style>
<div width="100%" align="center" id="categoriesmenu">
<span class="addcategory" onclick=""><?=$tracker_lang['Add'];?></span> | 
<span class="viewcategories selected" onclick="" ><?=$tracker_lang['View'];?></span>
</div>
<script type="text/javascript">
        jQuery('#categoriesmenu span').click(function(){
				if ( jQuery(this).hasClass('selected') ) return false;
                jQuery('#tabbedcontent div').hide();
                jQuery('#'+jQuery(this).attr("class")).show();
                jQuery('#categoriesmenu span').removeClass("selected");
                jQuery(this).addClass("selected");
                return false;
        });
		
</script>
<div id="tabbedcontent">
<div class="hidden" id="addcategory">
<?php 
echo '<h2>'.$tracker_lang['Add_new_category'].'</h2>
	<form name="form1" method="get" action="admincp.php">
	<table class="main" cellspacing="0" cellpadding="5" width="100%">
	<tr><td width="80">'.$tracker_lang['caption'].': </td><td align="left"><input type="text" size="50" name="cat_name"></td></tr>
	<tr><td>'.$tracker_lang['image'].': </td><td align="left"><input type="text" size="50" name="cat_img">
	<input type="hidden" name="op" value="AddCategory"></td></tr>
	<tr><td>'.$tracker_lang['sort'].': </td><td align="left"><input type="text" size="50" name="cat_sort"></td></tr>
	<tr><td colspan="2" align="right"><input type="submit" value="'.$tracker_lang['create'].'"></td></tr>
	</table></form>';
?>
</div>
<div id="viewcategories">
<?php
echo '<h2>'.$tracker_lang['Existed_categories'].'</h2>
<table class="main" cellspacing="0" cellpadding="5" width="100%">
<tr align="center"><td width="20" ><b>ID</b></td>
<td width="30"><b>'.$tracker_lang['weight'].'</b></td>
<td><b>'.$tracker_lang['caption'].'</b></td>
<td align="center" width="100"><b>'.$tracker_lang['image'].'</b></td>
<td width="30"><b>--</b></td>
<td width="30"><b>--</b></td>
<td width="30"><b>--</b></td>
</tr>';
	$query = "SELECT * FROM categories ORDER BY sort";
	$sql = sql_query($query) or sqlerr(__FILE__, __LINE__);
	while ($row = mysql_fetch_array($sql)) {
		$id = (int) $row['id'];
		$sort = $row['sort'];
		$name = $row['name'];
		$img = $row['image'];
		echo('<tr align="center"><td align="center">'.$id.'</td> 
<td>'.$sort.'</td> 
<td align="left"><strong>'.$name.'</strong></td> 
<td><img src="'.$DEFAULTBASEURL.$options['pic_base_url'].'cats/'.$img.'" border="0" /></td>
<td><a href="browse.php?cat='.$id.'">
<img src="'.$DEFAULTBASEURL.$options['pic_base_url'].'viewnfo.gif" border="0" /></a>
</td> <td><a href="admincp.php?op=EditCategory&id='.$id.'">
<img src="'.$DEFAULTBASEURL.$options['pic_base_url'].'multipage.gif" border="0"  /></a></td> 
<td><div align="center"><a onclick="" href="admincp.php?op=DeleteCategory&id='.$id.'">
<img src="'.$DEFAULTBASEURL.$options['pic_base_url'].'warned2.gif" border="0" align="center"/></a></td>
</tr>');
	}
	echo '</table></div></div><br />';
}

function UpdateCategory() {
	global $tracker_lang;
	$id=intval($_GET['id']);
	$cat_name = htmlspecialchars($_GET['cat_name'],ENT_QUOTES);
	$cat_img = htmlspecialchars($_GET['cat_img'],ENT_QUOTES);
	$cat_sort = intval($_GET['cat_sort']);
	$query = "UPDATE categories SET name = ".sqlesc($cat_name).", image = ".sqlesc($cat_img).", sort = ".sqlesc($cat_sort)." WHERE id=".sqlesc($id);
	$sql = sql_query($query) or sqlerr(__FILE__,__LINE__);
	if ( $sql ) {
		$data=array('title'=>$tracker_lang['success'],'text'=>$tracker_lang['Category was updated'],'type'=>'success');
		setcookie('infomsg',serialize($data));
		RedirectTo('admincp.php?op=Categories','script');
		cache_unset('categories');
	} else {
		$data=array('title'=>$tracker_lang['error'],'text'=>$tracker_lang['Incorrect ID'],'type'=>'error');
		setcookie('infomsg',serialize($data));
		RedirectTo('admincp.php?op=Categories','script');	
	}
}

switch ($op) {
    case 'Categories': 
		ViewCategories(); 
    break;
	case 'AddCategory':
		AddCategory();
	break;
    case 'EditCategory': 
		EditCategory(); 
    break;
	case 'UpdateCategory':
		UpdateCategory();
	break;
    case 'DeleteCategory': 
		DeleteCategory();
    break;

}
}
?>